package com.zjb.demo.system.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.mybatisflex.core.paginate.Page;
import com.zjb.demo.common.domain.dto.SysLoginInfo;
import com.zjb.demo.common.domain.response.AjaxResult;
import com.zjb.demo.system.domain.SysDictType;
import com.zjb.demo.system.domain.query.SysDictTypeQuery;
import com.zjb.demo.system.service.SysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Context;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * 数据字典信息
 *
 * @author 庄佳彬
 * @since 2023/12/28 12:20
 */
@Controller
@Mapping("/system/dict/type")
@Api(value = "数据字典信息")
public class SysDictTypeController {
    @Inject
    private SysDictTypeService typeService;

    @Get
    @Mapping("/list")
    @ApiOperation(value = "字典类型列表")
    public AjaxResult<Page<SysDictType>> list(SysDictTypeQuery query) {
        return AjaxResult.success(typeService.pageList(query));
    }

    @Get
    @Mapping("/export")
    @ApiOperation(value = "导出字典类型列表")
    public void export(SysDictTypeQuery query) throws IOException {
        var list = typeService.list(query);
        var ctx = Context.current();
        ctx.contentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        ctx.headerAdd("Content-Disposition", "attachment;filename=" + URLEncoder.encode("字典类型", StandardCharsets.UTF_8) + DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx");
        var os = ctx.outputStream();
        EasyExcel.write(os, SysLoginInfo.class)
                .sheet("字典类型")
                .doWrite(list);
    }

    @Get
    @Mapping("/{dictId}")
    @ApiOperation(value = "查询字典类型详细")
    public AjaxResult<SysDictType> detail(Long dictId) {
        return AjaxResult.success(typeService.getById(dictId));
    }

    @Post
    @Mapping
    @ApiOperation(value = "添加字典类型")
    public AjaxResult<Boolean> add(@Body SysDictType dict) {
        if (typeService.checkDictTypeUnique(dict)) {
            return AjaxResult.fail("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        return AjaxResult.success(typeService.save(dict));
    }

    @Put
    @Mapping
    @ApiOperation(value = "修改字典类型")
    public AjaxResult<Boolean> edit(@Body SysDictType dict) {
        if (typeService.checkDictTypeUnique(dict)) {
            return AjaxResult.fail("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        return AjaxResult.success(typeService.updateAndCache(dict));
    }

    @Delete
    @Mapping("/{dictIds}")
    @ApiOperation(value = "删除字典类型")
    public AjaxResult<Void> delete(Long[] dictIds) {
        typeService.deleteDictTypeByIds(dictIds);
        return AjaxResult.success();
    }

    @Delete
    @Mapping("/refreshCache")
    @ApiOperation(value = "刷新缓存")
    public AjaxResult<Void> refreshCache() {
        typeService.resetDictCache();
        return AjaxResult.success();
    }
}
